﻿using System;
using System.Text.RegularExpressions;



namespace WQ.Common
{
    /// <summary>
    /// 正则表达式代码
    /// 此类为抽象类，不允许实例化，在应用时直接调用即可
    /// </summary>
    public abstract class RegexHelper
    {

        /// <summary>
        /// 判断所输入的字符串是否从某个字符开始匹配类正则表达式
        /// </summary>
        /// <param name="objString">要匹配的字符串</param>
        /// <param name="regString">正则字符串（如：^[1-9]{1}$）</param>
        /// <param name="startNum">所要匹配的开始位置</param>
        /// <returns>返回真假值，true：匹配；false：不匹配</returns>
        public static bool isFitRegex(String objString, String regString, int startNum)
        {
            Regex myReg = new Regex(regString);
            return myReg.IsMatch(objString, startNum);
        }

        /// <summary>
        /// 判断字符串是否为正整数
        /// </summary>
        /// <param name="objString">要匹配的字符串</param>
        /// <returns>返回真假值，true：匹配；false：不匹配</returns>
        public static bool IsInt(String objString)
        {
            Regex myReg = new Regex(@"^\d+$");
            return myReg.IsMatch(objString);
        }
        /// <summary>
        /// 判断输入的字符串是否全是英文（不区分大小写）
        /// </summary>
        /// <param name="objString">所要匹配的字符串</param>
        /// <returns>返回真假值，true：匹配；false：不匹配</returns>
        public static bool isEnglishString(String objString)
        {
            Regex myReg = new Regex(@"^[a-zA-Z]+$");
            return myReg.IsMatch(objString);
        }
        /// <summary>
        /// 判断所输入的字符串是否为中文
        /// </summary>
        /// <param name="objString">所要匹配的字符串</param>
        /// <returns>返回真假值，true：匹配；false：不匹配</returns>
        public static bool isChinese(String objString)
        {
            Regex myReg = new Regex(@"^[\u4e00-\u9fa5]+$");
            return myReg.IsMatch(objString);
        }
        /// <summary>
        /// 判断输入字符串是否为英文及数字（英文不区分大小写）
        /// </summary>
        /// <param name="objString">所要匹配的字符串</param>
        /// <returns>返回真假值，true：匹配；false：不匹配</returns>
        public static bool isEngNum(String objString)
        {
            Regex myReg = new Regex(@"^[a-zA-Z0-9]+$");
            return myReg.IsMatch(objString);
        }
        public static bool isEngNumSymbol(String objString)
        {
            Regex myReg = new Regex(@"^[a-zA-Z0-9~!@#$%&-_\.,]+$");
            return myReg.IsMatch(objString);
        }
        /// <summary>
        /// 判断输入字符串是否为英文，数字，中文（英文不区分大小写）
        /// </summary>
        /// <param name="objString">所要匹配的字符串</param>
        /// <returns>返回真假值，true：匹配；false：不匹配</returns>
        public static bool isChineseEngNum(String objString)
        {
            Regex myReg = new Regex(@"^[\u4e00-\u9fa5a-zA-Z0-9]+$");
            return myReg.IsMatch(objString);
        }
        public static bool isChineseEngNumSymbol(String objString)
        {
            Regex myReg = new Regex(@"^[\u4e00-\u9fa5a-zA-Z0-9~!@#$%&-_\.,]+$");
            return myReg.IsMatch(objString);
        }
        /// <summary>
        /// 判断输入字符串是否为小数
        /// </summary>
        /// <param name="objString">所要匹配的字符串</param>
        /// <returns>返回真假值，true：匹配；false：不匹配</returns>
        public static bool isFloat(String objString)
        {
            Regex myReg = new Regex(@"^[0-9]+[.][0-9]+|[0-9]+$");
            return myReg.IsMatch(objString);
        }
        public static bool isPostCode(String objString)
        {
            if (objString == "")
                return false;
            Regex myReg = new Regex(@"^[1-9]\d{5}$");
            return myReg.IsMatch(objString);
        }
        /// <summary>
        /// 判断字符串是否符合此正则表达试
        /// </summary>
        /// <param name="str">所要匹配的字符串</param>
        /// <param name="regString">正则字符串（如：^[1-9]{1}$）</param>
        /// <returns>返回真假值，true：匹配；false：不匹配</returns>
        public static bool IsFitStrings(String str, String regString)
        {
            Regex objPattern = new Regex(regString);
            bool returnValue = objPattern.IsMatch(str);
            return returnValue;
        }

        /// <summary>
        /// 判断字符串是否为手机号或小灵通号
        /// </summary>
        /// <param name="telNumber">所要匹配的字符串</param>
        /// <returns>返回真假值，true：匹配；false：不匹配</returns>
        public static bool IsMobile(string telNumber)
        {
            if (telNumber == "")
                return false;
            Regex myReg = new Regex(@"^((\d{11,12})|(\d{7}))$");
            return myReg.IsMatch(telNumber);
        }

        /// <summary>
        /// 判断字符串是否为Email地址
        /// </summary>
        /// <param name="email">所要匹配的字符串</param>
        /// <returns>返回真假值，true：匹配；false：不匹配</returns>
        public static bool IsEmail(string email)
        {
            if (email == "")
            {
                return false;
            }
            Regex myReg = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
            return myReg.IsMatch(email);
        }

        /// <summary>
        /// 判断字符串是否为座机(如xxx-xxxxxxx-xxx)
        /// </summary>
        /// <param name="tel">所要匹配的字符串</param>
        /// <returns>返回真假值，true：匹配；false：不匹配</returns>
        public static bool IsTel(string tel)
        {
            if (tel == "")
                return false;
            Regex myReg = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?((\d{7,8})|(\d{11,12}))(-\d{1,5})?$");
            return myReg.IsMatch(tel);
        }
        /// <summary>
        /// 判断字符串是否为身份证号码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsIdentityCard(string str)
        {
            if (str == "")
            {
                return false;
            }
            else
            {
                Regex myReg = new Regex(@"(^\d{15}$)|(^\d{17}([0-9]|X)$)");
                return myReg.IsMatch(str);
            }
        }
        public static bool IsUrl(string url)
        {
            if (url == "")
            {
                return false;
            }
            Regex myReg = new Regex(@"^(http(s)?://)?([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
            return myReg.IsMatch(url);
        }

        /// <summary>
        /// 检测输入的字符串中是否含有,@#$%^&*'\"<>?|+= 字符
        /// </summary>
        /// <param name="str">待检测的字符串</param>
        /// <returns>有返回true，无返回false</returns>
        public static bool IsNonlicet(string str)
        {
            if (str == "")
            {
                return false;
            }
            Regex myReg = new Regex("[,()@$%^&*'\"<>?;=]");
            return myReg.IsMatch(str);
        }

        /// <summary>
        /// 是否是合法的日期时间格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDateTime(string str)
        {
            if (str == "")
            {
                return false;

            }
            Regex myReg = new Regex(@"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
            return myReg.IsMatch(str);
        }

        /// <summary>
        /// 是否合法的时间格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsTime(string str)
        {
            if (str == "")
            {
                return false;

            }
            Regex myReg = new Regex(@"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$");
            return myReg.IsMatch(str);
        }
    }
}
